package com.yanqu.road.dao.impl.activity.luckyshop;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.luckyshop.config.LuckyShopDrawConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LuckyShopConfigDaoImpl extends TempDao {

    private static String getLuckyShopDrawConfigSql = "select * from `t_s_activity_luckyshop_draw` where `activityId`=? ORDER BY `id` ASC";

    public Map<Integer, LuckyShopDrawConfig> getLuckyShopDrawConfig(int activityId) {
        Map<Integer, LuckyShopDrawConfig> result = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getLuckyShopDrawConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                LuckyShopDrawConfig data = new LuckyShopDrawConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("Id"));
                data.setReward(rs.getString("reward"));
                data.setWeight(rs.getInt("weight"));
                data.setBig(rs.getInt("big"));
                result.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

}
